<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <title>新增知识讲堂</title>
    <link rel="stylesheet" href="/plugins/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="/css/global.css" media="all">
    <link rel="stylesheet" href="/plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="/css/table.css" />
    <script type="text/javascript" src="/plugins/layui/layui.js"></script>
    <script type="text/javascript" src="/js/global-config.js"></script>
    <script charset="utf-8" src="/plugins/kindeditor/kindeditor-all.js"></script>
    <script charset="utf-8" src="/plugins/kindeditor/lang/zh-CN.js"></script>
    <script charset="utf-8" src="/plugins/kindeditor/plugins/code/prettify.js"></script>
    <script charset="utf-8" src="/js/kindEditor.js"></script>
    <style>
        .layui-form-label {
            width: 10%;
        }

        .layui-input-block {
            margin-left: 13%;
        }

        img {
            width: 120px;
            height: 120px;
        }

        .layui-upload-list {
            margin-left: 13%;
        }
    </style>
</head>
<body>
<div style="margin: 15px;">
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>新增知识讲堂(<label class="layui-required-tip">标红的为必填项</label>)</legend>
    </fieldset>
    <form class="layui-form" id="layui-form" action="" method="post">
        <div class="layui-form-item">
            <label class="layui-form-label layui-required-tip">标题：</label>
            <div class="layui-input-block">
                <input type="text" id="articleTitle" name="articleTitle" lay-verify="required" placeholder="标题" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-required-tip">内容：</label>
            <div class="layui-input-block">
                <textarea id="articleContent" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-required-tip">封面：</label>
            <div class="layui-input-block">
                <button type="button" class="layui-btn" id="test">
                    <i class="layui-icon">&#xe67c;</i>封面
                </button>
            </div>
            <div class="layui-upload-list">
                <img class="layui-upload-img" id="logoImg">
                <input type="hidden"  name="articleImg" id="articleImg"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">发布时间：</label>
            <div class="layui-input-block">
                <input type="radio" name="publishMode" value="1" lay-filter="pub-time" title="立即" checked="checked" />
                <input type="radio" name="publishMode" value="2" lay-filter="pub-time" title="定时" />
                <div id="publish-time" class="layui-inline">
                    <input class="layui-input" name="publishTime" id="publishTime" placeholder="默认采用当前时间"
                           onclick="layui.laydate.render({elem: this,min:0,type: 'datetime',show: true,closeStop: this, format: 'yyyy-MM-dd HH:mm:ss'})">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">是否置顶：</label>
            <div class="layui-input-block" id="setTop" >
                <input type="radio" name="setTop" value="1" title="是" >
                <input type="radio" name="setTop" value="0" title="否" checked>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="save">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary" >重置</button>
                <button type="button" class="layui-btn layui-btn-primary" onclick="javascript:history.back();" >返回</button>
            </div>
        </div>
    </form>
</div>
<script type="text/javascript" src="/plugins/layui/layui.js"></script>
<script type="text/javascript" src="/js/global-config.js"></script>
<script>
    layui.use(['jquery','table','laydate','layer','form','util','layedit','upload','element'],function(){
        var $=layui.jquery,table=layui.table,laydate = layui.laydate,
            layer = layui.layer,form = layui.form,util=layui.util,
            layedit=layui.layedit,upload = layui.upload;

        //初始化kindeditor编辑器
        var keditor=initEditor("articleContent");

        //时间选择器
        var start = {
            max: '2099-06-16 23:59:59',
            format: 'yyyy-MM-dd HH:mm:ss',
            type: 'datetime',
            min:util.toDateString(new Date().toString(), 'yyyy-MM-dd HH:mm:ss')
        };

        $('#publishTime').on('click',function (){
            start.elem = this;
            laydate.render(start);
        });

        form.on('radio(pub-time)',function (data) {
            showHide("publish-time", data.value == "2");
        });

        showHide("publish-time",false);

        function showHide(id, show) {
            var str = "#"+id;
            if (show){
                $(str).show();
            }else{
                $(str).hide();
            }
            form.render();
        }

        //执行实例
        var uploadlogo = upload.render({
            url: '/manage/upload/uploadFile',
            elem: '#test', //指定原始元素，默认直接查找class="layui-upload-file"
            size: 1000,
            before: function(obj) {
                layer.load();
            },
            done: function(res){
                //上传完毕回调
                $('#logoImg').attr('src', res.data["imgUrl"]); //图片链接（base64）
                $('#articleImg').val(res.data["key"]);
                layer.closeAll('loading');
            }
            ,error: function(){
                //请求异常回调
            }
        });
        //表单提交
        form.on('submit(save)', function(data){
            if(keditor.count('text') == 0||keditor.count('html') == 0){
                layer.msg("请输入文章内容！", {icon: 5, shift: 6});
                keditor.focus();
                return false;
            }
            if(data.field.articleImg==''){
                layer.msg("请上传封面图片！", {icon: 5, shift: 6});
                return false;
            }
            if(data.field.publishMode =="2" && data.field.publishTime == ""){
                layer.msg("请填写发布时间！", {icon: 5, shift: 6});
                return false;
            }
            layer.confirm('确认要提交吗？', {
                btn : [ '确定', '取消' ]//按钮
            }, function(index) {
                //同步编辑器内容到原来textarea
                keditor.sync('#articleContent');
                data.field.articleContent=keditor.html();
                $.ajax({
                    url:"/manage/kroom/insKlroom",
                    type:"post",
                    data:data.field,
                    dataType:"json",
                    success:function (data) {
                        layer.alert(data.msg, {
                            skin: 'layui-layer-molv' //样式类名  自定义样式
                            ,closeBtn: 1    // 是否显示关闭按钮
                            ,anim: 1 //动画类型
                            ,btn: ['确定'] //按钮
                            ,icon: data.code>=0?1:2    // icon
                            ,yes:function(index){
                                if (data.code >= 0) {
                                    location.href = "/manage/kroom/klroom";
                                }else{
                                    layer.close(index);
                                }
                            }
                        });
                    }
                })
            });
            return false;
        });
    });
</script>
</body>
</html>